<?php

class Ialbum_Api_Photo extends Core_Api_Abstract
{
  	public function getLikeCount($photo_id){
  		$photoTable = Engine_Api::_()->getDbtable('photos', 'album');
	    $photoTableName = $photoTable->info('name');
	    $likeTable = Engine_Api::_()->getDbtable('likes', 'core');
	    $likeTableName = $likeTable->info('name');
	    
	    $select = $photoTable->select()
    					->setIntegrityCheck(false)
                    	->from($photoTableName, array())
                    	->join($likeTableName, $likeTableName . '.resource_id=' . $photoTableName . '.photo_id', array('count(*) as total'))
                    	->where($likeTableName .'.resource_type = ?', 'album_photo')
                    	->where($photoTableName. '.photo_id = ?', $photo_id);
        $data = $photoTable->fetchRow($select);
        return $data->total;
  	}
	public function getPhotoSelect(array $params)
	  {
	  	$photoTable = Engine_Api::_()->getDbtable('photos', 'album');
	    $select = $photoTable->select();
	    
	    if( !empty($params['album']) && $params['album'] instanceof Ialbum_Model_Album ) {
	      $select->where('album_id = ?', $params['album']->getIdentity());
	    } else if( !empty($params['album_id']) && is_numeric($params['album_id']) ) {
	      $select->where('album_id = ?', $params['album_id']);
	    }
	    $select->where('album_id <> 0');
	    if( !isset($params['order']) ) {
	      $select->order('order ASC');
	    } else if( is_string($params['order']) ) {
	      $select->order($params['order']);
	    }
	    
	    return $select;
	  }
	  
	  public function getPhotoPaginator(array $params)
	  {
	    return Zend_Paginator::factory($this->getPhotoSelect($params));
  	  }
}
